/**
 * @see https://leetcode-cn.com/problems/minimum-deletions-to-make-character-frequencies-unique/
 * @param {*} str 
 */
var minDeletions = function(str) {
  let i = 0;
  let map = {};
  while(i < str.length) {
    let code = str.charAt(i);
    if(map[code]) {
      map[code]++;
    } else {
      map[code] = 1;
    }
    i++;
  }
  let arr = Object.values(map);
  let set = new Set(arr);
  // 判断是否是重复数组
  if(set.size === arr.length) {
    return 0;
  }
  set.clear();
  let repeatItems = [];
  // 找出重复的数组项
  arr.forEach(item => {
    if (set.has(item)) {
      repeatItems.push(item)
    } else {
      set.add(item)
    }
  });
  console.log(repeatItems)
  let count = 0;
  // 循环重复数组，逐一减一，判断减掉后的数字是否存在重复，如果重复继续，到0的就抛弃掉
  for (i = 0; i < repeatItems.length; i++) {
    if(repeatItems[i] != 0) {
      while(set.has(repeatItems[i])) {
        repeatItems[i]--;
        count++;
      }
      repeatItems[i] > 0 && set.add(repeatItems[i])
      console.log(set)
    }
  }
  return count;
}

minDeletions("bbcebab");